﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crownwood.Magic.Controls;
using System.Windows.Forms;
using Bamboo.Core.Common.Reflection;
using TinyERP.Framework.Plugin;

namespace TinyERP.Framework.Controls
{
    public class TabContentPane : Crownwood.Magic.Controls.TabControl, IContentPane
    {
        public event ContentPaneSelectedHandler OnSelectedPane;
        public event EventHandler OnLostFocus;
        public TabContentPane()
            : base()
        {
            this.SelectionChanged += new EventHandler(OnTabContentPaneSelectionChanged);
            this.ClosePressed += new EventHandler(OnTabControlClosePressed);
            this.LostFocus += new EventHandler(OnTabContentPaneLostFocus);
        }

        private void OnTabContentPaneLostFocus(object sender, EventArgs e)
        {
            if (OnLostFocus != null)
            {
                OnLostFocus(this, e);
            }
        }
        protected void OnTabControlClosePressed(object sender, EventArgs e)
        {
            if (this.SelectedTab != null)
            {
                this.TabPages.Remove(this.SelectedTab);
            }
        }

        public IForm GetCurrentForm()
        {
            if (this.SelectedTab != null)
            {
                return this.SelectedTab.Control as IForm;
            }
            return null;
        }

        private void OnTabContentPaneSelectionChanged(object sender, EventArgs e)
        {
            if (this.SelectedTab != null && this.SelectedTab.Tag is ApplicationNode)
            {
                (this.SelectedTab.Control as IForm).RegisterShortcuts();
                ApplicationNode formNode = this.SelectedTab.Tag as ApplicationNode;
                if (OnSelectedPane != null)
                {
                    OnSelectedPane(this, formNode);
                }
            }
        }
        

        public void ShowPane(ApplicationNode formNode)
        {
            Crownwood.Magic.Controls.TabPage tabPage = FindTabOfForm(formNode);
            if (tabPage != null)
            {
                this.SelectedTab = tabPage;
            }
            else
            {
                AddTabFromNode(formNode);
            }
        }

        private void AddTabFromNode(ApplicationNode formNode)
        {
            UserControl userControl = PluginManager.GetInstance().CreateForm(formNode.UIClassName);
            if (userControl != null)
            {
                userControl.Dock = DockStyle.Fill;
                userControl.Visible = true;
                userControl.Tag = formNode;
                ((IForm)userControl).CloseDialog += new CloseDialogHandler(OnCloseForm);
                Crownwood.Magic.Controls.TabPage tabPage = new Crownwood.Magic.Controls.TabPage(formNode.Title, userControl);
                tabPage.Selected = true;
                tabPage.Tag = formNode;
                this.TabPages.Add(tabPage);
            }
        }


        private Crownwood.Magic.Controls.TabPage FindTabOfForm(ApplicationNode formNode)
        {
            foreach (Crownwood.Magic.Controls.TabPage tabPage in this.TabPages)
            {
                if (tabPage.Tag as ApplicationNode == formNode)
                {
                    return tabPage;
                }
            }
            return null;
        }

        private void OnCloseForm(Control control)
        {
            ApplicationNode formNode = control.Tag as ApplicationNode;
            Crownwood.Magic.Controls.TabPage tabPage = FindTabOfForm(formNode);
            if (tabPage != null)
            {
                this.TabPages.Remove(tabPage);
            }
        }
        
    }
}
